<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\behavior;

use data\model\UserModel;
use data\model\NsOrderGoodsModel;
use data\model\NsOrderModel;
use data\model\ProvinceModel;
use data\model\CityModel;
use data\model\NsGoodsModel;
use data\model\NsGoodsBrandModel;
use data\model\NsGoodsCategoryModel;
use data\model\NsGoodsSkuModel;
use data\model\AlbumPictureModel;
use data\model\YcUserModel;
use data\model\YcLogModel;
use data\extend\YangCai;
/**
 * Description of UserAddSuccess
 *
 * @author pcy
 */
class OrderCreateSuccess 
{
    public  function  run($params)
    {
        $orderId = $params['order_id'];
        
        $orderModel = new NsOrderModel();
        
        $orderGoodsModel = new NsOrderGoodsModel();
        
        $orderInfo = $orderModel->getInfo(['order_id'=>$orderId]);
        
        $orderGoods = $orderGoodsModel->getQuery(['order_id'=>$orderId],'*','order_goods_id asc');
        
        if ( empty( $orderInfo ) || empty( $orderGoods ) )
        {
            exit;
        }
        
        $provinceId = $orderInfo['receiver_province'];
        
        $cityId = $orderInfo['receiver_city'];
        
        $provinceModel = new ProvinceModel();
        
        $cityModel = New CityModel();
        
        $provinceInfo = $provinceModel->getInfo(['province_id'=>$provinceId]);
        
        $cityInfo = $cityModel->getInfo(['city_id'=>$cityId]);
        
        $province = empty( $provinceInfo ) ? '' : $provinceInfo['province_name'];
        
        $city = empty( $cityInfo ) ? '' : $cityInfo['city_name'];
        
        $orderCode = $orderInfo['order_no'];//订单编号
        
        $total = intval($orderInfo['order_money']*100) / 100;//订单总额
        
        $linkman = $orderInfo['receiver_name'];//收货人姓名
        
        $linkmobile = $orderInfo['receiver_mobile'];//收货人电话
        
        $deliveryaddress = $orderInfo['receiver_address'];//收货详细地址
        
        $buyerId = $orderInfo['buyer_id'];//买家ID
        
        $usercode = $this->_getYcUserCode($buyerId);
        
        $remark = $orderInfo['seller_memo'];//订单备注
        
        $paymethod = $this->_getPaymethod(3);
        
        $ispaid = 0;
        
        $createtime = date('Y-m-d H:i:s', $orderInfo['create_time']);
        
        $goodsList = array();
        
        foreach ( $orderGoods AS $one )
        {
            $goodsId = $one['goods_id'];
            
            $skuId = $one['sku_id'];
            
            $goodsclassguid = 0;
            
            $goodsclassname = '其他';
            
            $imgurl = '';
            
            $goodsModel = new NsGoodsModel();
            
            $goodsInfo = $goodsModel->getInfo(['goods_id'=>$goodsId]);
            
            if ( !empty( $goodsInfo ) )
            {
                $categoryId = $goodsInfo['category_id'];
                
                $brandId = $goodsInfo['brand_id'];
                
                $categoryModel = new NsGoodsCategoryModel();
                
                $categoryInfo = $categoryModel->getInfo(['category_id'=>$categoryId]);
                
                $goodsclassname = empty($categoryInfo) ? '其他' : $categoryInfo['category_name'];
            
                $brandModel = new NsGoodsBrandModel();
                
                $brandInfo = $brandModel->getInfo(['brand_id'=>$brandId]);
                
                $goodsbrandname = empty( $brandInfo ) ? '其他' : $brandInfo['brand_name'];
                
                $picId = $goodsInfo['picture'];
                
                $pictureModel = new AlbumPictureModel();
                
                $picInfo = $pictureModel->getInfo(['pic_id'=>$picId]);
                
                $imgurl = $_SERVER['HTTP_HOST'] . '/' . $picInfo['pic_cover_big'];
            }
            
            $total = intval($one['goods_money']*100) / 100;
            
            $price = intval($one['price'] * 100) / 100;
            
            $goodsSkuModel = new NsGoodsSkuModel();
            
            $skuInfo = $goodsSkuModel->getInfo(['sku_id'=>$skuId]);
            
            $marketprice = empty( $skuInfo ) ? $one['price'] : $skuInfo['market_price'];
            
            $originalprice = intval($marketprice * 100) / 100;
            
            $goodsurl = $_SERVER['HTTP_HOST'] . '/index.php?s=/goods/goodsinfo&goodsid='. $goodsId;
            
            $goods = array(
                'goodsname' => $one['goods_name'],
                'goodsid' => $one['order_goods_id'],
                'spu' => $one['goods_id'],
                'sku' => $one['sku_id'],
                'model' => $one['sku_name'],
                'goodsclassguid' => $goodsclassguid,
                'goodsclassname' => $goodsclassname,
                'goodsbrandname' => $goodsbrandname,
                'qty' => (int)$one['num'],
                'total' => $total,
                'price' => $price,
                'originalprice' => $originalprice,
                'imgurl' => $imgurl,
                'goodsurl' => $goodsurl
            );
            
            $goodsList[] = $goods;
        }

        $data = array(
            'total' => $total,
            'linkman' => $linkman,
            'linkmobile' => $linkmobile,
            'usercode' => $usercode,
            'remark' => $remark,
            'ispaid' => $ispaid,
            'url' => '',
            'city' => $city,
            'createtime' => $createtime,
            'deliveryaddress' => $deliveryaddress,
            'province' => $province,
            'paymethod' => $paymethod,
            'orderid' => $orderCode,
            'suborderlist' => array(
                array(
                    'total' => $total,
                    'suborderid' => $orderCode,
                    'url' => '',
                    'goodslist' => $goodsList
                )
            )
        );
        //推送日志
        $log = array(
            'order_id' => $orderId,
            'push_data' => json_encode($data),
            'push_time' => time(),
            'status' => 1
        );
        
        $logModel = new YcLogModel();
        
        $logModel->save($log);
        
        $rs = YangCai::pushOrder($data);
        
        $log = array(
            'status' => ($rs['returnmessage']['code']==1) ? 2 : 3,
            'return_data' => json_encode($rs)
        );
        
        $logModel->save($log);
    }
    
    private function _getYcUserCode( $uid )
    {
        $userModel = new UserModel();
        
        $userInfo = $userModel->getInfo(['uid'=>$uid]);
        
        $userPhone = empty( $userInfo ) ? '' : $userInfo['user_tel'];
        
        $ycUserModel = new YcUserModel();
        
        $ycUserInfo = $ycUserModel->getInfo(['phone'=>$userPhone]);
        
        $usercode = empty( $ycUserInfo ) ? '' : $ycUserInfo['usercode'];
        
        return $usercode;
    }
    
    private function _getPaymethod( $type )
    {
        $typeList = array
        (
            1 => '货到付款-公务卡',
            2 => '货到付款-支票',
            3 => '货到付款-转账汇款',
            4 => '货到付款-现金结算',
            5 => '在线支付',
            9 => '账期'
        );
        
        return isset( $typeList[$type] ) ? $type : 9;
    }
}
